package com.zzz.springcloud.controller;

import com.zzz.springcloud.entity.CommonResult;
import com.zzz.springcloud.entity.Payment;
import com.zzz.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping(value = "/payment/add")
    public CommonResult create(@RequestBody Payment payment) {
        int count = paymentService.add(payment);

        log.info("插入执行完成" + serverPort);
        if (count > 0) {
            return new CommonResult(200, "success " + serverPort);
        } else {
            return new CommonResult(500, "fail " + serverPort);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        Payment payment = paymentService.getById(id);
        int a = 1;
        log.info("查询完成!!!!!4444"  + serverPort);
        if (payment != null) {
            return new CommonResult<>(200, "success "  + serverPort, payment);
        } else {
            return new CommonResult<>(500, "fail "  + serverPort);
        }
    }

    @GetMapping(value = "/payment/feign/timeout")
    public String getFeignTimeOut() {
        try{
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {

        }
        return "8001";
    }
}
